#include "include/MessageParser.h"

#include <algorithm>

#include "core/Utility.h"
#include "include/SSModel.h"
#include "include/SSMoneyMarketModel.h"
#include "include/SSQBModel.h"
#include "include/rdMessage.h"
#include "include/ssLoginProto.h"
#include "util/base64.h"

namespace qb {

MessageParser::MessageParser(void) : sMsgBuffer(NULL), nIsStillReceving(0) {
  m_time_decode = 0.0;
  m_time_extract = 0.0;
  m_time_clean = 0.0;
  m_msg_length = 0;
  m_funcid = 0;
}

MessageParser::~MessageParser(void) { sMsgBuffer = NULL; }

void MessageParser::Clean() {
  static double total_time = 0;
  static double total_count = 0;
  msg.Clear();
  total_time += m_time_decode + m_time_extract + m_time_clean;
  total_count += 1;

  nIsStillReceving = 0;
  m_time_decode = 0.0;
  m_time_extract = 0.0;
  m_time_clean = 0.0;
  m_msg_length = 0;
  m_funcid = 0;
  memset(&msgheader, 0, sizeof(AckMsgHead));
}

void MessageParser::ExtractHeader(const ProtoMessage& msg) {
  msgheader.retcode = 1;
  ProtoMap header;
  if (msg.GetMap("AckMsgHead", header)) {
    msgheader.total = msgheader.idx = 0;
    header.getInt16("retcode", msgheader.retcode);
    header.getInt16("total", msgheader.total);
    header.getInt16("idx", msgheader.idx);
    FIELDCOPY(msgheader.desc, CParserTool::GetString(header, "desc"));
    header.GetFloat("exectime", msgheader.exectime);
    if (msgheader.idx + 1 < msgheader.total)
      nIsStillReceving = 1;
    else
      nIsStillReceving = 0;
  }
}

void* MessageParser::ExtractData(int funcId, const ProtoMessage& cmsg) {
  ExtractHeader(msg);

  ProtoMap msgoutcome;
  if (!cmsg.GetMap("AckMsgBody", msgoutcome)) return nullptr;

  return m_decodeFunc(funcId, WrapProtoMsg(msgoutcome), nullptr);
}

bool MessageParser::JudgeJavaReturnInfo(const ProtoMessage& msg) {
  int res = -1;          // 0--执行成功
  float fSeconds = 0.0;  // 服务端执行时间,秒数
  std::string strError;  // 错误信息

  msg.GetInt32("res", res);
  msg.GetFloat("q", fSeconds);
  // msg.CParserTool::GetString("e",strError);
  strError = CParserTool::GetString(msg, "e");
  if (res == 0)
    return true;
  else {
    // OutputDebugString(strError.c_str());
    return false;
  }
}

int MessageParser::GetMessageType(const char* sBuffer, int nLen,
                                  SSAckMsg& stAckMsg) {
  m_current_reqid = 0;
  stAckMsg.m_Count = 1;
  int iFuncID = 0;
  if (sBuffer) {
    sMsgBuffer = const_cast<char*>(sBuffer);
    int ret = msg.decode(sBuffer, nLen);
    m_msg_length = nLen;
    qb::base::msg::ProtoUtil::CheckMessage(msg, sBuffer, nLen);

    // get RequestID
    stAckMsg.m_RequestID = atoi(msg.getMessageID().c_str());
    m_current_reqid = stAckMsg.m_RequestID;
    iFuncID = CParserTool::GetFuncIDbyType(msg.getType());
    m_funcid = iFuncID;

    return iFuncID;
  } else
    return iFuncID;
}
bool MessageParser::GetCurrentMessage(int reqid, const ProtoMessage*& pmsg) {
  if (m_current_reqid == reqid || reqid == 0) {
    pmsg = &msg;
    return true;
  }
  return false;
}
bool MessageParser::GetCurrentHead(AckMsgHead& head) {
  head = msgheader;
  return true;
}

void* MessageParser::DecodeMessage(SSAckMsg* pMsg) {
  if (!sMsgBuffer || m_decodeFunc == nullptr) return nullptr;

  nIsStillReceving = 0;
  pMsg->m_MsgType = SSReqType_Message;
  void* pdata = ExtractData(pMsg->m_FuncID, msg);
  //----------------------------------------------------------------------------
  pMsg->m_ErrorCode = msgheader.retcode;
  pMsg->m_Index = msgheader.idx;
  pMsg->m_Count = msgheader.total;
  pMsg->m_IsReceving = nIsStillReceving;  // 用于判断是否结束接收新包
  FIELDCOPY(pMsg->m_Desc, msgheader.desc);  // 错误信息

  pMsg->m_pRet = pdata;
  return pdata;
}

void MessageParser::SetDecodeFunc(DecodeFunc f) { m_decodeFunc = f; }

int MessageParser::DecodeFilterMessage(const char* sBuffer,
                                       const size_t& nBufSize,
                                       BondsFilter& filters) {
  if (nBufSize <= 0) return 0;
  int nLen0 = nBufSize * 2;
  char* pb64decode = new (std::nothrow) char[nLen0];
  if (!pb64decode) return 0;
  pb64decode[0] = 0;
  size_t ndecode = 0;
  // decode base64
  decode_base64(sBuffer, nBufSize, pb64decode, nLen0, &ndecode);
  // unzip
  char* psZipBuf = new (std::nothrow) char[40960];
  if (!psZipBuf) {
    delete[] pb64decode;
    return 0;
  }
  psZipBuf[0] = 0;
  int nZipSize = 0;
  if (!rdMessage::bUnZip(pb64decode, ndecode, psZipBuf, &nZipSize)) {
    delete[] pb64decode;
    delete[] psZipBuf;
    return 0;
  }

  ProtoMessage msg;
  msg.decode(psZipBuf, nZipSize);
#ifdef USING_PROTO_ADAPTOR
  qb::base::msg::ProtoUtil::CheckMessage(msg, psZipBuf, nZipSize);
#endif
  ProtoMap bondfilter;
  bool bVal = 0;
  if (WrapProtoMsg(msg).GetMap("BondsFilterStream", bondfilter)) {
    bondfilter.GetInt32("ID", filters.m_nID);
    int filterType;
    bondfilter.GetInt32("FilterType", filterType);
    filters.m_nFilterType = (FilterType::eFilterType)filterType;
    FIELDCOPY(filters.m_sName, CParserTool::GetString(bondfilter, "Name"));
    FIELDCOPY(filters.m_sConditionGroupId,
              CParserTool::GetString(bondfilter, "ConditionGroupId"));
    CParserTool::MessageGetTime(bondfilter, "Time", filters.m_Time);
    FIELDCOPY(filters.m_sVersion,
              CParserTool::GetString(bondfilter, "Version"));

    // BLHCONDITION
    ProtoMap blhcondt;
    ProtoList sublist;
    size_t ncount, i = 0;
    if (bondfilter.GetMap("BLHCONDITION", blhcondt)) {
      FIELDCOPY(filters.stBlh.sCompanyid,
                CParserTool::GetString(blhcondt, "Companyid"));
      FIELDCOPY(filters.stBlh.sDiffPrice,
                CParserTool::GetString(blhcondt, "DiffPrice"));
      blhcondt.GetUInt32("BothFlg", filters.stBlh.nBothFlg);
      blhcondt.GetUInt32("NewFlg", filters.stBlh.nNewFlg);
      blhcondt.GetUInt32("HasFlg", filters.stBlh.nHasFlg);
      blhcondt.GetUInt32("MatFlg", filters.stBlh.nMatFlg);
      blhcondt.GetUInt32("Mortgt", filters.stBlh.nMortgt);
      blhcondt.GetUInt32("Crosmk", filters.stBlh.nCrosmk);
      blhcondt.GetUInt32("Interbank", filters.stBlh.nInterbank);
      blhcondt.GetUInt32("Exchange", filters.stBlh.nExchange);
      blhcondt.GetUInt32("SSE", filters.stBlh.nSSE);
      blhcondt.GetUInt32("SZE", filters.stBlh.nSZE);
      blhcondt.GetUInt32("BSE", filters.stBlh.nBSE);
      blhcondt.GetUInt32("Weight", filters.stBlh.nRight);
      blhcondt.GetUInt32("Municp", filters.stBlh.nOldMunicp);
      blhcondt.GetUInt32("nExpireInHoliday", filters.stBlh.nExpireInHoliday);
      FIELDCOPY(filters.stBlh.sBPrice,
                CParserTool::GetString(blhcondt, "BPrice"));
      FIELDCOPY(filters.stBlh.sOPrice,
                CParserTool::GetString(blhcondt, "sOPrice"));
      FIELDCOPY(filters.stBlh.sDate1,
                CParserTool::GetString(blhcondt, "Date1"));
      FIELDCOPY(filters.stBlh.sDate2,
                CParserTool::GetString(blhcondt, "Date2"));

      // UINT	nType[30];			// CBondContainer::ProdTypes
      // UINT	nTypeNew[30];			// CBondContainer::ProdTypes
      if (blhcondt.GetList("TypeNew", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nTypeNew), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("Type", filters.stBlh.nTypeNew[i++]);
        }
      } else if (blhcondt.GetList("Type", sublist)) {  // 有nTypeNew， 不读nType
        ncount =
            std::min((int)_countof(filters.stBlh.nTypeNew), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("Type", filters.stBlh.nTypeNew[i++]);
        }
      }

      // UINT	nNCDRating[BLH_WARRANTCOUNT]
      if (blhcondt.GetList("NCDRating", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nNCDRating), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("NCD", filters.stBlh.nNCDRating[i++]);
        }
      }
      // UINT	nRightEx[10];			// CBondContainer::RightTypeEx
      if (blhcondt.GetList("RightEx", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nRightEx), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("RightEx", filters.stBlh.nRightEx[i++]);
        }
      }

      if (blhcondt.GetList("Popular", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nPopular), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("Popular", filters.stBlh.nPopular[i++]);
        }
      }

      // UINT	nRange[10];			// CBondContainer::DateRanges
      if (blhcondt.GetList("Range", sublist)) {
        ncount = std::min((int)_countof(filters.stBlh.nRange), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("Range", filters.stBlh.nRange[i++]);
        }
      }

      // UINT	nIssuerRating[10];			//
      // CBondContainer::RightLevels
      if (blhcondt.GetList("Level", sublist)) {
        ncount = std::min((int)_countof(filters.stBlh.nIssuerRating),
                          sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("Level", filters.stBlh.nIssuerRating[i++]);
        }
      }

      // nOutlookRating[10]	//CBondContainer::OutlookRating
      if (blhcondt.GetList("OutlookRating", sublist)) {
        ncount = std::min((int)_countof(filters.stBlh.nOutlookRating),
                          sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("OutlookRating", filters.stBlh.nOutlookRating[i++]);
        }
      }

      // nCDCRating[10]			//CBondContainer::CDCRating
      if (blhcondt.GetList("CDCRating", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nCDCRating), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("CDCRating", filters.stBlh.nCDCRating[i++]);
        }
      }

      // nCSIRating				//CBondContainer::CSIRating
      if (blhcondt.GetList("CSIRating", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nCSIRating), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("CSIRating", filters.stBlh.nCSIRating[i++]);
        }
      }

      // nCBRRating[10]			//CBondContainer::CBRRating
      if (blhcondt.GetList("CBRRating", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nCBRRating), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("CBRRating", filters.stBlh.nCBRRating[i++]);
        }
      }

      // UINT	nIssueYear[10];
      if (blhcondt.GetList("IssueYear", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nIssueYear), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("IssueYear", filters.stBlh.nIssueYear[i++]);
        }
      }

      // UINT	nSpecial[10];		// CBondContainer::SpecialTypes
      if (blhcondt.GetList("CouponType", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nCouponType), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("CouponType", filters.stBlh.nCouponType[i++]);
        }
      } else if (blhcondt.GetList("Special", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nCouponType), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("Special", filters.stBlh.nCouponType[i++]);
        }
      }
      // UINT	nInstion[10];		// CBondContainer::InstionTypes
      if (blhcondt.GetList("Instion", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nInstion), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("Instion", filters.stBlh.nInstion[i++]);
        }
      }

      // UINT	nDebtRating[10];		// CBondContainer::DebtRating
      if (blhcondt.GetList("DebtRate", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nDebtRating), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("DebtRate", filters.stBlh.nDebtRating[i++]);
        }
      }

      // UINT	nFinDebt[10];		// CBondContainer::FinDebt
      if (blhcondt.GetList("FinDebt", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nFinDebt), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("FinDebt", filters.stBlh.nFinDebt[i++]);
        }
      }
      // UINT	nLocalBond[10];		// CBondContainer::LocalBond
      if (blhcondt.GetList("LocalBond", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nLocalBond), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("LocalBond", filters.stBlh.nLocalBond[i++]);
        }
      }
      // AreaChar	szCategoryFlag[BLH_ISSUEYEARCOUNT];	// 专项债
      if (blhcondt.GetList("CategoryFlag", sublist)) {
        ncount = std::min((int)_countof(filters.stBlh.szCategoryFlag),
                          sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          FIELDCOPY(filters.stBlh.szCategoryFlag[i++],
                    CParserTool::GetString(msgmap, "Flag"));
        }
      }
      // szIssuer
      if (blhcondt.GetList("Issuer", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.szIssuer), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          FIELDCOPY(filters.stBlh.szIssuer[i],
                    CParserTool::GetString(msgmap, "Issuer"));
          i++;
        }
      }

      // szSector
      if (blhcondt.GetList("Sector", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.szSector), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          FIELDCOPY(filters.stBlh.szSector[i],
                    CParserTool::GetString(msgmap, "Sector"));
          i++;
        }
      }

      // szSector2021
      if (blhcondt.GetList("Sector2021", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.szSector2021), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          FIELDCOPY(filters.stBlh.szSector2021[i],
                    CParserTool::GetString(msgmap, "Sector2021"));
          i++;
        }
      }

      // szArea
      if (blhcondt.GetList("Area", sublist)) {
        ncount = std::min((int)_countof(filters.stBlh.szArea), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          FIELDCOPY(filters.stBlh.szArea[i],
                    CParserTool::GetString(msgmap, "Area"));
          i++;
        }
      }

      // szSubType
      if (blhcondt.GetList("SubType", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.szSubType), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          FIELDCOPY(filters.stBlh.szSubType[i],
                    CParserTool::GetString(msgmap, "SubType"));
          i++;
        }
      }

      // nWarrant
      if (blhcondt.GetList("NewWarrant", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nWarrant), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("NewWarrant", filters.stBlh.nWarrant[i++]);
        }
      }

      // nAssetStatus[10]
      if (blhcondt.GetList("AssetStatus", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nAssetStatus), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("AssetStatus", filters.stBlh.nAssetStatus[i++]);
        }
      }

      // nMunicp
      if (blhcondt.GetList("NewMunicp", sublist)) {
        ncount = std::min((int)_countof(filters.stBlh.nMunicp), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("NewMunicp", filters.stBlh.nMunicp[i++]);
        }
      }
      // UINT    nPlatform[BLH_WARRANTCOUNT];		//平台 ： 全部 省级 市级
      // 区县级  其他
      if (blhcondt.GetList("Platform", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nPlatform), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("Platform", filters.stBlh.nPlatform[i++]);
        }
      }
      // MuniAreaChar szMuniArea[BLH_MUNIAREACOUNT];	// 城投地区
      if (blhcondt.GetList("MuniArea", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.szMuniArea), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          FIELDCOPY(filters.stBlh.szMuniArea[i++],
                    CParserTool::GetString(msgmap, "Area"));
        }
      }
      // MuniAreaChar szLocalBondArea[BLH_LOCALBONDAREACOUNT];		//
      // 地方债地区
      if (blhcondt.GetList("LocalBondArea", sublist)) {
        ncount = std::min((int)_countof(filters.stBlh.szLocalBondArea),
                          sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          FIELDCOPY(filters.stBlh.szLocalBondArea[i++],
                    CParserTool::GetString(msgmap, "Area"));
        }
      }
      // int		nHighYieldBond[20];		//高收益债
      if (blhcondt.GetList("HighYieldBond", sublist)) {
        ncount = std::min((int)_countof(filters.stBlh.nHighYieldBond),
                          sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetInt32("type", filters.stBlh.nHighYieldBond[i++]);
        }
      }
      // 永续债
      if (blhcondt.GetList("ETSBond", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nETSBond), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("ETSBond", filters.stBlh.nETSBond[i++]);
        }
      }
      // 三方担保
      if (blhcondt.GetList("ThirdPartyGuarantee", sublist)) {
        ncount = std::min((int)_countof(filters.stBlh.nThirdPartyGuarantee),
                          sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("type", filters.stBlh.nThirdPartyGuarantee[i++]);
        }
      }
      // 银行分类
      if (blhcondt.GetList("BankBond", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nBankBond), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetInt32("type", filters.stBlh.nBankBond[i++]);
        }
      }
      // 偿还方式
      if (blhcondt.GetList("PrincipalPayType", sublist)) {
        ncount = std::min((int)_countof(filters.stBlh.nPrincipalPayType),
                          sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetInt32("type", filters.stBlh.nPrincipalPayType[i++]);
        }
      }
      // 次级债
      if (blhcondt.GetList("SubOrdinate", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nSubOrdinate), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetInt32("type", filters.stBlh.nSubOrdinate[i++]);
        }
      }
      // 做市标的
      if (blhcondt.GetList("MarketMakeTarget", sublist)) {
        ncount = std::min((int)_countof(filters.stBlh.nMarketMakeTarget),
                          sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("type", filters.stBlh.nMarketMakeTarget[i++]);
        }
      }
      // 上市场所
      if (blhcondt.GetList("ListedPlace", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.nListedPlace), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          msgmap.GetUInt32("type", filters.stBlh.nListedPlace[i++]);
        }
      }

      // 债券流动性
      const char* bondLiquidity =
          CParserTool::GetString(blhcondt, "BondLiquidity");
      if (bondLiquidity && strlen(bondLiquidity) > 0) {
        FIELDCOPY(filters.stBlh.sBondLiquidity, bondLiquidity);
      }

      // 主体流动性
      const char* issuerLiquidity =
          CParserTool::GetString(blhcondt, "IssuerLiquidity");
      if (issuerLiquidity && strlen(issuerLiquidity) > 0) {
        FIELDCOPY(filters.stBlh.sIssuerLiquidity, issuerLiquidity);
      }

      // 担保人
      if (blhcondt.GetList("Guarantor", sublist)) {
        ncount =
            std::min((int)_countof(filters.stBlh.szGuarantor), sublist.size());
        i = 0;
        for (ProtoList::const_iterator it = sublist.begin();
             it != sublist.end(); ++it) {
          if (i == ncount) break;
          const ProtoMap& msgmap = *it;
          FIELDCOPY(filters.stBlh.szGuarantor[i],
                    CParserTool::GetString(msgmap, "Guarantor"));
          i++;
        }
      }
    }

    delete[] pb64decode;
    delete[] psZipBuf;
    return 1;
  } else {
    delete[] pb64decode;
    delete[] psZipBuf;
    return 0;
  }
}

}  // namespace qb
